using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_AllMember : System.Web.UI.Page
{
    #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + ADESSA.Entities.Member._Table + "].[" + MemberColumns.MemberID + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
    public string FullName
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["FullName"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["FullName"];
        }
    }
    public string Email
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Email"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["Email"];
        }
    }
    public long Sex
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Sex"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["Sex"]);
        }
    }
    public long Status
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Status"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["Status"]);
        }
    }
    public long Gold
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Gold"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["Gold"]);
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();
        LoadSex();
        LoadStatus();
        LoadGold();
        LoadData();
    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulAdmin = (HtmlGenericControl)Master.FindControl("ulMember");
        ulAdmin.Attributes.Add("class", "current");
        HtmlGenericControl liSubShow = (HtmlGenericControl)Master.FindControl("liSubShowMem");
        liSubShow.Attributes.Add("class", "sub_show");
        HtmlControl divSelectSub = (HtmlControl)Master.FindControl("divSelectSubMem");
        divSelectSub.Attributes.Add("class", "select_sub show");
    }
    #endregion
    #region LoadData
    private string username = string.Empty;
    private void LoadData()
    {
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        if (!string.IsNullOrEmpty(this.FullName))
            cri.add("[" + Member._Table + "].[" + MemberColumns.FullName + "]", this.FullName, Condition.LIKE, Condition.AND);
        if (!string.IsNullOrEmpty(this.Email))
            cri.add("[" + Member._Table + "].[" + MemberColumns.Email + "]", this.Email, Condition.LIKE, Condition.AND);
        if (this.Sex != long.MinValue && this.Sex != 0)

            cri.add("[" + Member._Table + "].[" + MemberColumns.Sex + "]", this.Sex, Condition.EQUAL, Condition.AND);
        if (this.Status != long.MinValue && this.Status != 2)
            cri.add("[" + Member._Table + "].[" + MemberColumns.Status + "]", this.Status, Condition.EQUAL, Condition.AND);
        if (this.Gold != long.MinValue && this.Gold != 2)
            cri.add("[" + Member._Table + "].[" + MemberColumns.GoldMember + "]", this.Gold, Condition.EQUAL, Condition.AND);
        MemberCollection _MemberCollection = MemberService.GetMemberListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);

        rptList.DataSource = _MemberCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;

    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Member _Member = (Member)e.Item.DataItem;
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            Literal ltrFullName = (Literal)e.Item.FindControl("ltrFullName");
            Literal ltrEmail = (Literal)e.Item.FindControl("ltrEmail");
            Literal ltrGold = (Literal)e.Item.FindControl("ltrGold");
            Literal ltrPhone = (Literal)e.Item.FindControl("ltrPhone");
            CheckBox ckEnable = (CheckBox)e.Item.FindControl("ckEnable");
            CheckBox chkGolg = (CheckBox)e.Item.FindControl("chkGold");
            ltrFullName.Text = _Member.FullName;
            ltrEmail.Text = _Member.Email;
            ltrPhone.Text = _Member.Phone;
            chkGolg.Checked = _Member.GoldMember;
            if (string.IsNullOrEmpty(_Member.ActivateCode))
                ckEnable.Checked = true;
            else
                ckEnable.Checked = false;
            if (_Member.GoldMember == true)
                ltrGold.Text = "True";
            else
                ltrGold.Text = "False";
            hidID.Value = _Member.MemberID.ToString();
            int STT = NoCountStart + e.Item.ItemIndex;
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "none";
            }
        }

    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string commandName = e.CommandName;
        object obj = new object();
        switch (commandName)
        {
            case "sortName":
                obj = "[" + Member._Table + "].[" + MemberColumns.FullName + "]";
                break;
            case "SortOrder":
                obj = "[" + Member._Table + "].[" + MemberColumns.Email + "]";
                break;
            default:
                break;
        }
        if (OrderDirection.ASC == OrderDirection.ASC)
            Session["Order"] = OrderDirection.DESC;
        else
            Session["Order"] = OrderDirection.ASC;
        this.hidColSort = obj.ToString();
        LoadData();
    }
    #endregion
    #region Delete Account
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            CheckBox chkDelete = (CheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);

                try
                {
                    #region Check Order data
                    Criteria cri = new Criteria();
                    cri.add(CompanyColumns.MemberID, ID, Condition.EQUAL, Condition.AND);
                    CompanyCollection _CompanyCollection = CompanyService.GetCompanyListByParam(CompanyColumns.CompanyID, OrderDirection.ASC, cri.Criter);
                    Criteria _cri = new Criteria();
                    _cri.add(ProductSellColumns.MemberId, ID, Condition.EQUAL, Condition.AND);
                    ProductSellCollection _ProductSellCollection = ProductSellService.GetProductSellListByParam(ProductSellColumns.ProductId, OrderDirection.ASC, _cri.Criter);
                    Criteria crit = new Criteria();
                    crit.add(ProductBuyColumns.MemberId, ID, Condition.EQUAL, Condition.AND);
                    ProductBuyCollection _ProductBuyCollection = ProductBuyService.GetProductBuyListByParam(ProductSellColumns.ProductId, OrderDirection.ASC, crit.Criter);
                    if (_ProductSellCollection.Count > 0 || _ProductBuyCollection.Count > 0 || _CompanyCollection.Count > 0)
                    {
                        lb_error.Text = "Dont's delete this item because it is getting using another items";
                    }
                    else
                    {
                        MemberService.DeleteMember(ID);
                    }
                    #endregion

                }
                catch
                {
                    lb_error.Text = "Dont's delete this item because it is getting using another items";
                }
            }
        }
        LoadData();
    }
    #endregion
    #region Update
    protected void linkUpdate_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");
            CheckBox ckEnable = (CheckBox)item.FindControl("ckEnable");
            Member _Member = MemberService.GetMember(long.Parse(hidID.Value));


            if (_Member != null)
            {
                if (ckEnable.Checked)
                    _Member.ActivateCode = string.Empty;

                MemberService.UpdateMember(_Member);
            }
        }
        LoadData();
    }

    #endregion
    #region Search
    protected void linkSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("AllMember.aspx?FullName={0}&Email={1}&Sex={2}&Status={3}&Gold={4}", txtFullNameSearch.Text.Trim(), txtEmailSearch.Text.Trim(), ddlSexSearch.SelectedValue, ddlStausSearch.SelectedValue, ddlGoldSearch.SelectedValue));
    }
    protected void linkSearchAll_Click(object sender, EventArgs e)
    {
        Response.Redirect("AllMember.aspx");
    }
    #endregion
    #region Load DDL Gender And Status
    protected void LoadSex()
    {

        ddlSexSearch.Items.Add(new ListItem("--Select--", "0"));
        ddlSexSearch.Items.Add(new ListItem("Male", "1"));
        ddlSexSearch.Items.Add(new ListItem("Female", "2"));
        ddlSexSearch.Items.Add(new ListItem("Mr.Ms", "3"));
    }
    protected void LoadStatus()
    {

        ddlStausSearch.Items.Add(new ListItem("--Select--", "2"));
        ddlStausSearch.Items.Add(new ListItem("Activated", "1"));
        ddlStausSearch.Items.Add(new ListItem("Not activated", "0"));

    }
    protected void LoadGold()
    {

        ddlGoldSearch.Items.Add(new ListItem("--Select--", "2"));
        ddlGoldSearch.Items.Add(new ListItem("True", "1"));
        ddlGoldSearch.Items.Add(new ListItem("False", "0"));

    }
    #endregion

}
